//
//  MemberInfoEditViewController.m
//  LiteraryTune
//
//  Created by 陈雪丹 on 2018/2/4.
//  Copyright © 2018年 cxd. All rights reserved.
//

#import "MemberInfoEditViewController.h"
#import "MemberCenterTopTableViewCell.h"
#import "MemberInfoEditTableViewCell.h"
#import "MemberInfoModel.h"

@interface MemberInfoEditViewController () <UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong)UITableView *myTableView;
@property (nonatomic, strong)MemberInfoModel *dataModel;


@end

@implementation MemberInfoEditViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"信息修改";
    [self getNetData];
    [self buildUI];
}

- (void)getNetData {
    [CXDNetHelp getRequestWithURLString:[NSString stringWithFormat:@"%@%@",GetInfoURL,[CXDHelper getUserAccountName]] parameters:nil success:^(id  _Nullable responseObject) {
        NSLog(@"%@",responseObject);
        _dataModel = [MemberInfoModel yy_modelWithJSON:responseObject];
        [self.myTableView reloadData];
    } failure:^(NSError * _Nullable error) {
        
    }];
}

- (void)buildUI {
    [self.view addSubview:self.myTableView];
}

#pragma mark - event response
- (void)confirmBtnClickAction {
    MemberInfoEditTableViewCell *cell = [self.myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    if (cell.nickNameTF.text.length == 0) {
        [CXDNoticeView showNoticeViewWithString:@"请输入昵称"];
    }else if (cell.realNameTF.text.length == 0) {
        [CXDNoticeView showNoticeViewWithString:@"请输入真实姓名"];
    }else if (cell.mailTF.text.length == 0) {
        [CXDNoticeView showNoticeViewWithString:@"请输入邮箱"];
    }else if (cell.telTF.text.length == 0) {
        [CXDNoticeView showNoticeViewWithString:@"请输入电话"];
    }else if (cell.addressTF.text.length == 0) {
        [CXDNoticeView showNoticeViewWithString:@"请输入地址"];
    }else if (cell.UserNameTF.text.length == 0) {
        [CXDNoticeView showNoticeViewWithString:@"请输入用户名"];
    }else {
        [CXDNetHelp getRequestWithURLString:InfoModifyURL parameters:@{@"nicheng":cell.nickNameTF.text,@"xingming":cell.realNameTF.text,@"email":cell.mailTF.text,@"tel":cell.telTF.text,@"address":cell.addressTF.text,@"username":cell.UserNameTF.text} success:^(id  _Nullable responseObject) {
//            NSLog(@"%@",responseObject);
            if ([[responseObject objectForKey:@"zhuangtai"] isEqualToString:@"1"]) {
                [CXDNoticeView showNoticeViewWithString:[responseObject objectForKey:@"message"]];
                [self.navigationController popViewControllerAnimated:YES];
            }else {
                [CXDNoticeView showNoticeViewWithString:[responseObject objectForKey:@"message"]];
            }
        } failure:^(NSError * _Nullable error) {
            
        }];
    }
}

#pragma mark - 懒加载
- (UITableView *)myTableView {
    if (!_myTableView) {
        _myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64) style:UITableViewStylePlain];
        _myTableView.delegate = self;
        _myTableView.dataSource = self;
        _myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_myTableView registerNib:[UINib nibWithNibName:NSStringFromClass([MemberCenterTopTableViewCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([MemberCenterTopTableViewCell class])];
        [_myTableView registerNib:[UINib nibWithNibName:NSStringFromClass([MemberInfoEditTableViewCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([MemberInfoEditTableViewCell class])];
    }
    return _myTableView;
}

#pragma mark - 协议实现
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//    if (indexPath.row == 0) {
//        MemberCenterTopTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([MemberCenterTopTableViewCell class])];
//        return cell;
//    }
    MemberInfoEditTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([MemberInfoEditTableViewCell class])];
    cell.UserNameTF.text = _dataModel.username;
    cell.nickNameTF.text = _dataModel.nicheng;
    cell.realNameTF.text = _dataModel.xingming;
    cell.mailTF.text = _dataModel.email;
    cell.telTF.text = _dataModel.tel;
    cell.addressTF.text = _dataModel.address;
    [cell.confirmBtn addTarget:self action:@selector(confirmBtnClickAction) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//    if (indexPath.row == 0) {
//        return 220;
//    }
    return 460;
}

@end
